package com.pbc.gmp.action;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.pbc.gmp.commons.base.BaseController;
import com.pbc.gmp.commons.utils.PageInfo;
import com.pbc.gmp.model.basemodel.Brand;
import com.pbc.gmp.model.basemodel.Equipmenttype;
import com.pbc.gmp.model.mybatisDto.BrandInfo;
import com.pbc.gmp.service.IBrandService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * InnoDB free: 9216 kB  前端控制器
 * </p>
 *
 * @author kjk
 * @since 2018-07-29
 */
@Controller
@RequestMapping("/brand")
public class BrandController extends BaseController {
    @Autowired
    private IBrandService brandService;
    @RequestMapping(value="/eqbrand")
    @ResponseBody
    public List<Brand> eqtype() {
        List<Brand> list =brandService.selectList(null);
        return list;
    }

    @RequestMapping(value="/getbrandbyeqtypeid")
    @ResponseBody
    public List<Brand> getBrandByEqtypeid(@RequestParam("id") Long id) {
        Wrapper whr=new EntityWrapper<>();
        whr.eq("equTypeId",id);
        List<Brand> list =brandService.selectList(whr);
        return list;
    }
    @RequestMapping(value="/brandList")
    @ResponseBody
    public Object brandList(Brand brandWhere, Integer page, Integer rows, String sort, String order) {
        PageInfo pageInfo = new PageInfo(page, rows, sort, order);
        Map<String, Object> condition = new HashMap<String, Object>();

        if (brandWhere.getBrandname()!=null) {
            condition.put("brandname", brandWhere.getBrandname());
        }
        if (brandWhere.getEqutypeid()!=null) {
            condition.put("equtypeid", brandWhere.getEqutypeid());
        }


        pageInfo.setCondition(condition);
        brandService.selectBrandByDetailWhere(pageInfo);
        return pageInfo;

    }
    @RequestMapping(value="/brandManger")
    public String brandManger() {

        return "service/equipment/brandManger";
    }
    @RequestMapping(value="/addPage")
    public String addPage() {
        return "service/equipment/brandAdd";
    }
    @RequestMapping(value="/editPage")
    public String editPage(Model model, @RequestParam Long id) {

        Brand brand    = brandService.selectById(id);
        model.addAttribute("brand",brand );


        return "service/equipment/brandEdit";

    }
    @RequestMapping(value = "/update")
    @ResponseBody
    public Object add(Brand bra) {
        if (bra.getId() == null) {
            bra.setId(0);
            brandService.insert(bra);
        }else {
            brandService.updateById(bra);
        }

        return renderSuccess("更新成功");
    }
    @RequestMapping(value = "/delete", method = {RequestMethod.POST })
    @ResponseBody
    public Object delete(String ids) {
        List<Long > idlist= Arrays.asList(ids.split(",")).stream().map(s -> Long.parseLong(s.trim())).collect(Collectors.toList());

        brandService.deleteBatchIds(idlist);
        return renderSuccess("删除成功！");
    }

}
